package com.glory.cqaospda.viewmodel

import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.model.TaskRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.google.gson.JsonObject

class Startjob2Model: BaseViewModel() {

    private val repository by lazy { TaskRepository() }

    fun defaultRunLotHis(IsTempProgram: String,StepName: String, Action: String, LotId: String,RLotId: String, EqpID: String,passNum:String,mark:String,lead:String,IsNewLot: Boolean) {
        val map = JsonObject()
        map.addProperty("LotNo", LotId)
        map.addProperty("RLotNo", if (RLotId.equals(LotId)) LotId else RLotId)
        map.addProperty("Action", Action)
        map.addProperty("StepName", StepName)
        map.addProperty("EquipmentName", Constant.eqpid)
        map.addProperty("passNumBer", passNum)
        map.addProperty("Mark", mark)
        map.addProperty("Lead", lead)
        map.addProperty("IsTempProgram", IsTempProgram)
        map.addProperty("IsNewLot", if (IsNewLot)"1" else "0")
        launchWithError({
            val defaultRunLotHis =
                repository.defaultRunLotHis(map,"EMS_RunLotHis")
            when(Action){
                "End"->{
                    defUI.callObserve.value = CallObserveBean(2,defaultRunLotHis)
                }
                "Stop"->{
                    defUI.callObserve.value = CallObserveBean(3,defaultRunLotHis)
                }
                "Start"->{
                    defUI.callObserve.value = CallObserveBean(1,defaultRunLotHis)

                }
            }

        },{},true)
    }

    fun defaultEndRun(map: JsonObject) {
        launchOnlyResult({
            val endRunBean = repository.defaultEndRun(map,"EMS_EndRun")
            defUI.callObserve.value = CallObserveBean(4,endRunBean)
        })
    }

    fun defaultLotInfo(lotID: String,EquipmentName:String) {
        val map = JsonObject()
        map.addProperty("Lotid", lotID)
        map.addProperty("EquipmentName", EquipmentName)
        launchOnlyResult({
            val defaultLotInfo =
                repository.defaultLotInfo(map,"EMS_GetLotInfo")
            defUI.callObserve.value = CallObserveBean(5,defaultLotInfo)
        })
    }

    fun defaultFinalTestProgram(lotID: String,EquipmentName:String,StepName:String) {
        val map = JsonObject()
        map.addProperty("LotNo", lotID)
        map.addProperty("EquipmentName", EquipmentName)
        map.addProperty("StepName", StepName)
        launchOnlyResult({
            val defaultLotInfo =
                repository.defaultFinalTestProgram(map,"EMS_FinalTestProgram")
            defUI.callObserve.value = CallObserveBean(6,defaultLotInfo)
        })
    }

    fun defaultEqpMaterials(eqpID: String) {
        val map = JsonObject()
        map.addProperty("EquipmentName", eqpID)
        launchOnlyResult({
            val defaultEqpMaterials =
                repository.defaultEqpMaterials(map,"EMS_EqpMaterials")
            defUI.callObserve.value = CallObserveBean(7,defaultEqpMaterials)
        })
    }

    fun defaultGetResultData(map: JsonObject) {
        launchOnlyResult({
            val resultData = repository.defaultGetResultData(map,"EMS_GetResultData")
            defUI.callObserve.value = CallObserveBean(8,resultData)
        })
    }

}